class DscoresController < ApplicationController
  # GET /dscores
  # GET /dscores.json
  def index
    @dscores = Dscore.all

    respond_to do |format|
      format.html # index.html.erb
      format.json  { render :json => @dscores }
    end
  end

  # GET /dscores/1
  # GET /dscores/1.json
  def show
    @dscore = Dscore.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json  { render :json => @dscore }
    end
  end

  # GET /dscores/new
  # GET /dscores/new.json
  def new
    @dscore = Dscore.new

    respond_to do |format|
      format.html # new.html.erb
      format.json  { render :json => @dscore }
    end
  end

  # GET /dscores/1/edit
  def edit
    @dscore = Dscore.find(params[:id])
  end

  # POST /dscores
  # POST /dscores.json
  def create
    @dscore = Dscore.new(params[:dscore])

    respond_to do |format|
      if @dscore.save
        format.html { redirect_to(@dscore, :notice => 'Dscore was successfully created.') }
        format.json  { render :json => @dscore, :status => :created, :location => @dscore }
      else
        format.html { render :action => "new" }
        format.json  { render :json => @dscore.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /dscores/1
  # PUT /dscores/1.json
  def update
    @dscore = Dscore.find(params[:id])

    respond_to do |format|
      if @dscore.update_attributes(params[:dscore])
        format.html { redirect_to(@dscore, :notice => 'Dscore was successfully updated.') }
        format.json  { head :ok }
      else
        format.html { render :action => "edit" }
        format.json  { render :json => @dscore.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /dscores/1
  # DELETE /dscores/1.json
  def destroy
    @dscore = Dscore.find(params[:id])
    @dscore.destroy

    respond_to do |format|
      format.html { redirect_to(dscores_url) }
      format.json  { head :ok }
    end
  end

  def getDscoresByReservationid
    @dscores = Dscore.where(:resid => params[:resid])
    respond_to do |format|
      format.json  { render :json => @dscores }
    end
  end

  def updateDscore
    @dscore = Dscore.find(params[:id])
    @dscore.rate = params[:rate]
    @dscore.save
    @dish = Dish.find(@dscore.dish_id)
    @dish.rate = Dscore.average(:rate, :conditions => {:dish_id => @dscore.dish_id})
    @dish.save

    respond_to do |format|
      format.json  { render :json => @dscore }
    end
  end

end
